using UnityEngine;
using System.Collections;

public class CannonController: MonoBehaviour 
{

	GameObject m_gMainBall;
	LineRenderer m_LRLineRender;
	float m_LineSections = 10.0f;
	float TimeStart = 0.0f;
	bool m_bLaunch = false;
	
	void OnDrawGizmos()
	{
		Gizmos.DrawIcon(transform.FindChild("BarrelEndPoint").transform.position,"decal_icon.png");
		Gizmos.DrawIcon(transform.FindChild("MiddlePoint").transform.position, "decal_icon.png");
		Gizmos.DrawIcon(transform.FindChild("LandingPoint").transform.position, "decal_icon.png");
	}
	// Use this for initialization
	void Start () 
	{
		FindMainBall();
		m_LRLineRender = GetComponent<LineRenderer>();
		m_LRLineRender.SetVertexCount((int)m_LineSections);
	}
	void FindMainBall()
	{
		foreach(GameObject g in GameObject.FindGameObjectsWithTag("Ball"))
		{
			if(g.GetComponent<BallController>().IsMain)
			{
				m_gMainBall = g;
				
			}
		}
		
	}
	// Update is called once per frame
	void Update () 
	{
		
		if(m_gMainBall)
		{
			float dist = Vector3.Distance(transform.FindChild("LaunchPlatform").transform.position, m_gMainBall.transform.position);
			if(dist < transform.FindChild("LaunchPlatform").GetComponent<CapsuleCollider>().radius + m_gMainBall.GetComponent<SphereCollider>().radius)
			{
				
					TimeStart = Time.time;
				m_bLaunch = true;
				
			}
			if( m_bLaunch)
			{
				LaunchBall();
			}
			Plot(transform.FindChild("BarrelEndPoint").transform.position, transform.FindChild("MiddlePoint").transform.position, transform.FindChild("LandingPoint").transform.position);
		}
		else
		{
			FindMainBall();
		}
	}
	void LaunchBall()
	{
		//m_gMainBall.transform.position += Vector3.Lerp(m_gMainBall.transform.position, transform.FindChild("TranslationPoint").transform.position, Time.deltaTime);
		//if(AlmostEqualTo(m_gMainBall.transform.position, transform.FindChild("TranslationPoint").transform.position))
		//{
			Vector3 _VecToCannonEnd = transform.FindChild("BarrelEndPoint").transform.position - transform.FindChild("TranslationPoint").transform.position;
			Vector3 _VecToLandingPoint = transform.FindChild("LandingPoint").transform.position - transform.FindChild("TranslationPoint").transform.position;
			m_gMainBall.transform.position = GetCoordinates(Mathf.Lerp(0.0f,1.0f, Time.time - TimeStart),  transform.FindChild("BarrelEndPoint").transform.position, transform.FindChild("MiddlePoint").transform.position, transform.FindChild("LandingPoint").transform.position);
			//m_gMainBall.GetComponent<Rigidbody>().useGravity = false;
		//}
		if(m_gMainBall.transform.position == transform.FindChild("LandingPoint").transform.position)
		{
			m_bLaunch = false;
			print("landed");
			m_gMainBall.GetComponent<Rigidbody>().useGravity = true;
			
		}
	}
		 bool AlmostEqualTo(Vector3 _Vec1, Vector3 _Vec2)
		   {
			if(Mathf.Approximately(_Vec1.x, _Vec2.x) && Mathf.Approximately(_Vec1.y, _Vec2.y) && Mathf.Approximately(_Vec1.z, _Vec2.z))
			{
				return true;
			}
			return false;
		}
	Vector3 GetCoordinates(float _t, Vector3 _StartPoint, Vector3 _CenterPoint, Vector3 _EndPoint)
	{
		return Mathf.Pow(1-_t, 2) * _StartPoint + 2 *_t*(1-_t)*_CenterPoint + Mathf.Pow(_t, 2)*_EndPoint;
	}
void Plot(Vector3 _StartPoint, Vector3 _CenterPoint, Vector3 _EndPoint)
	{
		float _t = 0.0f;
		for(int i = 0; i < m_LineSections; ++i)
		{
			_t = (i/(m_LineSections-1));
			      
			m_LRLineRender.SetPosition(i, GetCoordinates(_t, _StartPoint, _CenterPoint, _EndPoint));
		}
	}


}
